function graphsVec=plotColsOrPages(pivotMat,varMat,inStru,nameStru)
% =========================================================================
%% plotDecompPerPage(varMat,obsnames,shonames,title,offset);
%
% 1) *pivotMat*    [nobs NC] matrix of data.
%
% 2) *varMat*      [nobs NC NP] matrix which decomposes each column of
%                  pivot Mat across pages 
%
% 3) *inStru*      Structure with all inputs for the graph, 
%                  including 
% Required
%
%  .'orgBy'         Organize by 'cols' or 'pages'
%  .'legend'        Of the two lines per plot 
%
% Optional 
%  .'xaxis'        Vector of X-axis 
%  .'suptitle'     String to add to the dimension being ploted in titles 
% *nameStru*  .colNames 
%                .pageNames
%
%
% Alejandro Justiniano January 21st 2010
% =========================================================================
[NR NC NP]=size(varMat); 
if size(pivotMat,1)~=NR;
    error('Rows Pivot do not match Var Matrix') 
end
if size(pivotMat,2)~=NC;
    error('Column Pivot do not match Var Matrix') 
end
%% 3. Defaults
[~,inStru]=ch_field(inStru,'xOffset',0.1); 
[~,inStru]=ch_field(inStru,'yOffset',0.1); 
[~,inStru]=ch_field(inStru,'flagBoxOff',1); 
[~,inStru]=ch_field(inStru,'lineWidths',[1.5 2.5]); 
[~,inStru]=ch_field(inStru,'suptitle',[]); 
[~,inStru]=ch_field(inStru,'suptitleFlush','left'); 
[~,inStru]=ch_field(inStru,'xaxis',(1:NR)'); 
%% NRows and NCols defined below 
inStru=plotAssignDefaults(inStru);
%% 2. Check sizes
if length(nameStru.colNames)~=NC; 
    error('colNames does not match number of columns in CMAT'); 
end 
if length(nameStru.pageNames)~=NP && ~(isfield(inStru,'override') && inStru.override==1); 
    error('pageNames does not match number of pages in CMAT'); 
end 
if isfield(inStru,'legend')==false; 
    error('Need provide inStru.legend'); 
end 
if isfield(inStru,'orgBy')==false; 
    disp('inStru.orgBy must be cols or pages'); 
    error('Need provide inStru.orgBy'); 
end 
%% Organize Plots by Columns or Pages? 
if strmatch(upper(inStru.orgBy),'COLS'); 
    flagOrgByCol=1; 
    NOuter=NC; 
    NInner=NP; 
elseif strmatch(upper(inStru.orgBy),'PAGES'); 
    flagOrgByCol=0; 
    NOuter=NP; 
    NInner=NC; 
else 
    error('Need define inStru.orgBy'); 
end 
[~,inStru]=ch_field(inStru,'NFigRows',min(4,ceil(NInner/inStru.NFigCols))); 
[~,inStru]=ch_field(inStru,'NFigCols',2); 


if ~isempty(inStru.axesFontSize); 
    set(0,'DefaultAxesFontSize',inStru.axesFontSize);
end 

%% Graph Settings 
NRows=inStru.NFigRows;
NCols=inStru.NFigCols;
graphsVec =zeros(ceil(NInner/(NRows*NCols)),NOuter);

for jj=1:NOuter
    ISubplots=0;
    IGraphs  =0;
    ICounter =0;
    %% Begin Inner Loop 
    for ii=1:NInner
        if ISubplots==0
            IGraphs=IGraphs+1;
            graphsVec(IGraphs,jj)=figure;
        end
        ISubplots=ISubplots+1;
        ICounter =ICounter+1;
        subplot(NRows,NCols,ISubplots);
        if ISubplots==NRows*NCols || ii==NInner
            inStru.flagNoLegend=0;
        else
            inStru.flagNoLegend=1;
        end
        if flagOrgByCol==true
            tempMat=[pivotMat(:,jj) squeeze(varMat(:,jj,ii))];
            tempTit=nameStru.pageNames{ii};             
            tempSuptit=char(nameStru.colNames{jj}); 
        else
            tempMat=[pivotMat(:,ii) squeeze(varMat(:,ii,jj))];
            tempTit=nameStru.colNames{ii};
            tempSuptit=char(nameStru.pageNames{jj});
        end 
        if isempty(inStru.suptitle)==false;
            if strmatch(upper(inStru.suptitleFlush),'left')==1;
                tempSuptit=[char(inStru.suptitle),char(tempSuptit)];
            else
                tempSuptit=[char(tempSuptit),char(inStru.suptitle)];
            end
        end
        [~,~,~]=plot3sampSub(inStru.xaxis,tempMat,tempTit,1,inStru);
        if ii==NInner || ISubplots==NRows*NCols; 
            ISubplots=0;
            if size(graphsVec,1)==1
                supHand=suptitle(tempSuptit);
            else
                supHand=suptitle([tempSuptit,'   (',num2str(IGraphs),' of ',...
                    num2str(size(graphsVec,1)),')']);
            end
            set(supHand,'FontSize',inStru.suptitleFontSize,'FontName',inStru.FontName,...
                'FontWeight','Bold');
            set(graphsVec(IGraphs,jj),'PaperPosition',inStru.paperPosition)
        end
    end
    if ~isempty(inStru.axesFontSize);
        set(0,'DefaultAxesFontSize','remove');
    end
end 